package com.tevintao.marketingsystem.task;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookWindowManagerGlobal;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import org.json.JSONObject;


/**
 * 通过搜索添加好友，
 * 电话号码，微信号，昵称。
 */
public class AddFriendBySearchTaskProcess extends SetTaskExtra {
    private static AddFriendBySearchTaskProcess ourInstance = new AddFriendBySearchTaskProcess();

    public static AddFriendBySearchTaskProcess getInstance() {
        return ourInstance;
    }

    private AddFriendBySearchTaskProcess() {
    }

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            WxUiViewConfig.clickSearchButton((ViewGroup) v);
        } else if (WxUiViewConfig.VIEW_MAIN_LAUNCHER_SEARCH.equals(contextName)) {
            View editText = v.findViewById(WxUiViewConfig.ID_SEARCH_EDIT_TEXT);
            if (editText != null && editText instanceof EditText) {
                Object searchName = getExtra(WxUiViewConfig.KEY_SEARCH_FRIEND_KEY_WORDS);
                if (null == searchName || TextUtils.isEmpty(searchName.toString())) {
                    taskError(v.getContext(), WxTaskManger.TASK_SATE_EXTRA_ERROR, "没有搜索的关键字");
                    return true;
                } else {
                    TextView inputView = (TextView) editText;
                    if (searchName.toString().equals(inputView.getText().toString())) {
                        LogManager.getInstance().logInfo("已经输入内容");

                        View searchResult = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.ID_SEARCH_RESULT_CONTENT, "查找手机/QQ号:" + searchName.toString());
                        LogManager.getInstance().logInfo("searchResult = " + searchResult);
                        if (null != searchResult) {
                            ViewOperation.clickView(searchResult);
                        }
                    } else {
                        ViewOperation.setText(editText, getExtra(WxUiViewConfig.KEY_SEARCH_FRIEND_KEY_WORDS).toString());
                    }
                }
            }
        } else if (WxUiViewConfig.VIEW_CONTACT_INFO_UI.equals(contextName)) {
            JSONObject joTree = HookWindowManagerGlobal.GetViewJsonTree((ViewGroup) v, -1);
            HookWindowManagerGlobal.printViewTreeJson(joTree, 0);
            View send = v.findViewById(WxUiViewConfig.ID_CONTACTS_INFO_SEND);
            if (send != null && send.getVisibility() == View.VISIBLE) {
                taskError(v.getContext(), WxTaskManger.TASK_SATE_TASK_PAST, "已经是好友了");
            }
            View sayHi = v.findViewById(WxUiViewConfig.ID_CONTACTS_INFO_SAY_HI);
//            LogManager.getInstance().logInfo("sayhi = " + sayHi + " Visibility = " + sayHi.getVisibility());
            if (sayHi != null && sayHi.getVisibility() == View.VISIBLE) {
                ViewOperation.clickView(sayHi);
            }
            View addContacts = v.findViewById(WxUiViewConfig.ID_CONTACTS_INFO_ADD);
//            LogManager.getInstance().logInfo("addContacts = " + addContacts + " Visibility = " + addContacts.getVisibility());
            if (addContacts != null && addContacts.getVisibility() == View.VISIBLE) {
                ViewOperation.clickView(addContacts);
//                taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "添加好友成功");
            }
        } else if (WxUiViewConfig.DIALOG_CLASS.equals(contextName)) {
            View info = v.findViewById(WxUiViewConfig.DIALOG_INFO_TEXT);
            if (null != info) {
                TextView tv = (TextView) info;
                String promptMessage = tv.getText().toString();
                if ("用户不存在".equals(promptMessage)) {
                    LogManager.getInstance().logError("搜索的用户不存在");
                    taskError(v.getContext(), WxTaskManger.TASK_SATE_FIND_FRIEND_ERROR, "搜索的用户不存在");
                }
            }
        } else if (WxUiViewConfig.VIEW_SEARCH_FRIEND_SAY_HI.equals(contextName)) {
            String sayHiMsg = "";

            Object sayHiObj = getExtra(WxUiViewConfig.KEY_CHAT_CONTENT);
            if (null != sayHiObj) {
                sayHiMsg = sayHiObj.toString();
            }
            if (!TextUtils.isEmpty(sayHiMsg)) {
                View sayContent = v.findViewById(WxUiViewConfig.KEY_SEARCH_FRIEND_SAY_HI_TEXT_VIEW);
                if (null != sayContent && sayContent instanceof TextView) {
                    ViewOperation.setText(sayContent, sayHiMsg);
                }
            }

            View sendView = v.findViewById(WxUiViewConfig.KEY_SEARCH_FRIEND_SAY_HI_SEND);
            if (null != sendView) {
                ViewOperation.clickView(sendView);
                taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "添加好友成功");
            }
        }
        return false;
    }
}
